package com.imooc.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @author shkstart
 * @create 2022-03-09 22:57
 */
@Configuration
public class CorsConfig {
    public CorsConfig() {
    }

    @Bean
    public CorsFilter corsFilter() {
        //添加cors配置信息
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOrigin("*");
        config.addAllowedOrigin("http://localhost:8080");
        config.addAllowedOrigin("http://8.130.26.26:8080");
        config.addAllowedOrigin("http://8.130.26.26");
        config.addAllowedOrigin("http://8.130.26.26:8888");
        config.addAllowedOrigin("http://8.130.26.26:22122");
        config.addAllowedOrigin("http://8.130.26.26:6379");
        config.addAllowedOrigin("http://8.130.26.26:9300");
        config.addAllowedOrigin("http://shop.haowangzhan.top");
        config.addAllowedOrigin("http://shop.haowangzhan.top:8080");
        config.addAllowedOrigin("http://center.haowangzhan.top");
        config.addAllowedOrigin("http://center.haowangzhan.top:8080");
        config.addAllowedOrigin("http://host.haowangzhan.top");
        config.addAllowedOrigin("http://host.haowangzhan.top:8088");

        //设置是否发送cookie信息
        config.setAllowCredentials(true);
        //设置允许请求的方式
        config.addAllowedMethod("*");
        //设置允许的header
        config.addAllowedHeader("*");
        //为url添加映射路径
        UrlBasedCorsConfigurationSource corsSource = new UrlBasedCorsConfigurationSource();
        corsSource.registerCorsConfiguration("/**", config);

        //返回重新定义好的corsSource
        return new CorsFilter(corsSource);
    }
}
